package com.asurplus.common.realname.util;

import java.util.Base64;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class ImageToBase64 {

    public static void main(String[] args) {
        try {
            test("D://原始图片.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static String test(String imagePath) throws IOException {
        // 图片文件路径
       // String imagePath = "D://原始图片.jpg";

        // 读取图片文件到字节数组
        Path path = new File(imagePath).toPath();
        byte[] imageBytes = Files.readAllBytes(path);

        // 对字节数组进行Base64编码
        String base64ImageString = Base64.getEncoder().encodeToString(imageBytes);

        // 输出Base64编码后的字符串
        System.out.println(base64ImageString);
        return base64ImageString;
    }
}